package 
{
	import Box2D.Collision.b2AABB;
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.b2Body;
	
	import com.util.Camera;
	
	import starling.core.Starling;
	import starling.display.MovieClip;
	import starling.events.Event;

	public class tinku extends GameObject implements ICollidable
	{
		private var tinkuRun:MovieClip;
		private var _world:World;
		private var physBody:b2Body;
		private var isDead:Boolean;
		
		public function tinku(data:Object, texID:uint = -1)
		{
			super();
			isDead = false;
			_world = data as World;
	
			tinkuRun = new MovieClip(Assets.getLevelMapAtlas().getTextures(_world.getTMXTileMap().tileMapper[texID]),12);
			
			addChild(tinkuRun);
			this.addEventListener(starling.events.Event.ADDED_TO_STAGE, onAddedToStage);
		}
					
		private function onAddedToStage(event:Event):void
		{
			this.removeEventListener(starling.events.Event.ADDED_TO_STAGE, null);
			
			init();
		}
						
		public function init():void
		{
			physBody = _world.createPhysBody(this.x, this.y-this.height, this.width, this.height, true, "enemy");
			World.juggler.add(this.tinkuRun);
			collisionmanager.addCollidable(this);
		}
		
		public override function onEnterFrame(dt:Number):void
		{
			if(!isDead)
			{
				var physPos:b2Vec2 = physBody.GetPosition();
				this.x = m2p(physPos.x) - this.width/2; 
				this.y = m2p(physPos.y) + this.height/2;	
				
				if(physBody.GetLinearVelocity().x > -2)
					physBody.ApplyImpulse(new b2Vec2(-1, 0), physBody.GetWorldCenter());				
			}
		}		
						
		public function onColliding(tgt:ICollidable):void
		{
			isDead = true;
			collisionmanager.removeCollidable(this);
			_world.removePhysBody(physBody);
			World.juggler.remove(this.tinkuRun);
			this.removeChild(tinkuRun);			
			tinkuRun = new MovieClip(Assets.getskeleton().getTextures("Effects/enemyDie_"),12);
			tinkuRun.loop = false;
			this.addChild(tinkuRun);
			World.juggler.add(this.tinkuRun);
			tinkuRun.addEventListener(Event.COMPLETE, onDeathAnimCompleted);
		}
		
		private function onDeathAnimCompleted():void
		{
			//bullet hit !! die die die....
			removeFromParent(true);			
		}
		
		public function get camera():Camera
		{
			return _world.camera;
		}

		public function get collisionID():uint
		{
			return collisionmanager.TINKU;	
		}
				
		private function m2p(meters:Number):Number
		{
			return meters * Game.pixelsPerMeter;
		}

		public function intersectsLine(x1:Number, x2:Number):Boolean
		{
			var objX1:Number = x;
			var objX2:Number = objX1 + this.width - 1;
			return (objX1 > x1 || objX2 > x1);	/**Collision test for objects to the right are turned off for now because 4 columns 
			* to the right of camera that are not in view are cached in advance for continuity
			**/ //&& (objX1 < x2 || objX2 < x2); 
		}

		public override function dispose():void 
		{			
			if(!isDead)
			{
				collisionmanager.removeCollidable(this);
				_world.removePhysBody(physBody);				
			}
			super.dispose();			
			World.juggler.remove(this.tinkuRun);
			tinkuRun = null;									 					
		}
	}	
}